package com.sunxiansheng.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunxiansheng.user.entity.PageResult;
import com.sunxiansheng.user.entity.dto.UserDto;
import com.sunxiansheng.user.entity.po.UserPo;
import com.sunxiansheng.user.mapper.UserMapper;
import com.sunxiansheng.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description:
 * @Author sun
 * @Create 2024/7/9 16:45
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public int addUser(UserDto userDto) {
        // 封装数据
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userDto, userPo);
        int insert = userMapper.insert(userPo);
        return insert;
    }

    @Override
    public void delete(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public PageResult<UserPo> getUserPage(UserDto userDto) {
        Page<UserPo> userPoPage = new Page<>(userDto.getPageIndex(), userDto.getPageSize());
        IPage<UserPo> userPage = userMapper.getUserPage(userPoPage);
        PageResult<UserPo> pageResult = new PageResult<>();
        pageResult.loadData(userPage);
        return pageResult;
    }

}